 import java.util.*;

//学生成绩类
class StudentScore {
    private String name;        
    private String studentId;   
    private String course;      
    private double score;       

    public StudentScore(String name, String studentId, String course, double score) {
        this.name = name;
        this.studentId = studentId;
        this.course = course;
        this.score = score;
    }

    public String getName() { return name; }
    public String getStudentId() { return studentId; }
    public String getCourse() { return course; }
    public double getScore() { return score; }
}

public class GradeManager {
    private static List<StudentScore> scores = new ArrayList<>(); 
    private static Scanner scanner = new Scanner(System.in);       

    public static void main(String[] args) {
        while (true) {
            printMenu();          
            int choice = getValidInt("请输入选项序号：", 1, 4); 

            switch (choice) {
                case 1:
                    recordScore();  
                    break;
                case 2:
                    queryScore();   
                    break;
                case 3:
                    statCourse();   
                    break;
                case 4:
                    System.out.println("感谢使用学生成绩管理系统，再见！");
                    return; 
            }
        }
    }

    private static void printMenu() {
        System.out.println("\n=================================");
        System.out.println("欢迎使用学生成绩管理系统");
        System.out.println("=================================");
        System.out.println("请选择操作：");
        System.out.println("1. 记录学生成绩");
        System.out.println("2. 查询学生成绩");
        System.out.println("3. 统计课程成绩");
        System.out.println("4. 退出系统");
    }

    private static void recordScore() {
        System.out.println("\n===== 记录学生成绩 =====");
        String name = inputNonEmpty("请输入学生姓名：");
        String studentId = inputNonEmpty("请输入学生学号：");

        if (isDuplicateId(studentId)) {
            System.out.println("错误：该学号已存在！");
            return;
        }

        String course = inputNonEmpty("请输入课程名称：");
        double score = getValidScore(); 

        scores.add(new StudentScore(name, studentId, course, score));
        System.out.println("成绩已成功记录！");
    }

    private static boolean isDuplicateId(String id) {
        return scores.stream().anyMatch(s -> s.getStudentId().equals(id));
    }

    private static void queryScore() {
        System.out.println("\n===== 查询学生成绩 ======");
        System.out.println("请选择查询方式：");
        System.out.println("1. 按学生姓名查询");
        System.out.println("2. 按学生学号查询");
        System.out.println("3. 按课程名称查询");
        int type = getValidInt("请输入选项序号：", 1, 3);
        String keyword = inputNonEmpty("请输入查询关键字：");

        List<StudentScore> results = new ArrayList<>();
        switch (type) {
            case 1: results = scores.stream().filter(s -> s.getName().equals(keyword)).toList(); break;
            case 2: results = scores.stream().filter(s -> s.getStudentId().equals(keyword)).toList(); break;
            case 3: results = scores.stream().filter(s -> s.getCourse().equals(keyword)).toList(); break;
        }

        if (results.isEmpty()) System.out.println("未找到相关记录！");
        else results.forEach(s -> System.out.printf("姓名：%s, 学号：%s, 课程：%s, 成绩：%.1f%n", 
            s.getName(), s.getStudentId(), s.getCourse(), s.getScore()));
    }

    private static void statCourse() {
        System.out.println("\n===== 统计课程成绩 ======");
        String course = inputNonEmpty("请输入课程名称：");
        List<Double> scoresList = scores.stream().filter(s -> s.getCourse().equals(course))
            .mapToDouble(StudentScore::getScore).boxed().toList();

        if (scoresList.isEmpty()) {
            System.out.println("该课程暂无成绩记录！");
            return;
        }

        double avg = scoresList.stream().mapToDouble(Double::doubleValue).average().orElse(0);
        double max = scoresList.stream().mapToDouble(Double::doubleValue).max().orElse(0);
        double min = scoresList.stream().mapToDouble(Double::doubleValue).min().orElse(0);

        System.out.printf("课程：%s%n平均分：%.2f%n最高分：%.1f%n最低分：%.1f%n", 
            course, avg, max, min);
    }

    private static String inputNonEmpty(String prompt) {
        String str;
        do {
            System.out.print(prompt);
            str = scanner.nextLine().trim();
            if (str.isEmpty()) System.out.println("错误：输入不能为空！");
        } while (str.isEmpty());
        return str;
    }

    private static double getValidScore() {
        double score;
        while (true) {
            System.out.print("请输入成绩（0-100）：");
            if (scanner.hasNextDouble()) {
                score = scanner.nextDouble();
                if (score >= 0 && score <= 100) return score;
            } else scanner.next(); 
            System.out.println("错误：成绩必须为0-100之间的数字！");
        }
    }

    private static int getValidInt(String prompt, int min, int max) {
        int num;
        while (true) {
            System.out.print(prompt);
            if (scanner.hasNextInt()) {
                num = scanner.nextInt();
                if (num >= min && num <= max) return num;
            } else scanner.next(); 
            System.out.printf("错误：请输入%d-%d之间的整数！%n", min, max);
        }
    }
}